package retrofitdemo.com.night.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableInt;
import androidx.recyclerview.widget.RecyclerView;

import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import okhttp3.Call;
import okhttp3.Response;
import retrofitdemo.com.night.R;
import retrofitdemo.com.night.bean.CommendCallback;
import retrofitdemo.com.night.databinding.ItemCommendBinding;
import retrofitdemo.com.night.utils.GlideUtil;
import retrofitdemo.com.night.utils.HTTPAPI;
import retrofitdemo.com.night.utils.LogUtils;
import retrofitdemo.com.night.utils.OnItemClickListener;

public class CommendAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<CommendCallback.ResultBean> strings;
    private Activity mcontext;
    OnItemClickListener mOnItemClickLitener;

    public ObservableBoolean FBI_COLLECT = new ObservableBoolean(false);
    public ObservableBoolean FBI_LIKE = new ObservableBoolean(false);
    public ObservableBoolean FBI_NO_LIKE = new ObservableBoolean(false);

    public CommendAdapter(Activity context, List<CommendCallback.ResultBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemCommendBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_commend, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;

        //1：喜欢 2：不喜欢 3：收藏
        tv.ivModuleCollect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                doWithCommint(strings.get(position).getId(),"3");
            }
        });

        tv.ivModuleNLike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                doWithCommint(strings.get(position).getId(),"2");
            }
        });

        tv.ivModuleLike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                doWithCommint(strings.get(position).getId(),"1");
            }
        });

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<CommendCallback.ResultBean> {
        ItemCommendBinding functionBinding;
        CheckBox ivModuleCollect, ivModuleLike, ivModuleNLike;

        public TourViewHolder(ItemCommendBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModuleCollect = dataBinding.ivModuleCollect;
            ivModuleLike = dataBinding.ivModuleLike;
            ivModuleNLike = dataBinding.ivModuleNLike;
        }

        @Override
        public void bindData(CommendCallback.ResultBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    /**
     * 1：喜欢 2：不喜欢 3：收藏
     *
     * @param id
     * @param type
     */
    public void doWithCommint(String id, final String type) {
        HTTPAPI.getInstance().doWithCommint(mcontext, id, type, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("type-->" + type);
                LogUtils.e("操作-->" + s);
                Toast.makeText(mcontext, "操作完成", Toast.LENGTH_SHORT).show();
            }
        });
    }
}